
USE [HaVy]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Sua table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_Get_List

AS


				
				SELECT
					[MaSua],
					[TenSua],
					[GiaNhap],
					[GiaTrenBaoBi],
					[GiaBan],
					[HinhAnh],
					[ThongTin],
					[MaLoai],
					[MaNSX],
					[SoLuong]
				FROM
					[dbo].[Sua]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Sua table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [MaSua] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([MaSua])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [MaSua]'
				SET @SQL = @SQL + ' FROM [dbo].[Sua]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[MaSua], O.[TenSua], O.[GiaNhap], O.[GiaTrenBaoBi], O.[GiaBan], O.[HinhAnh], O.[ThongTin], O.[MaLoai], O.[MaNSX], O.[SoLuong]
				FROM
				    [dbo].[Sua] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[MaSua] = PageIndex.[MaSua]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Sua]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Sua table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_Insert
(

	@MaSua int    OUTPUT,

	@TenSua nvarchar (100)  ,

	@GiaNhap numeric (18, 0)  ,

	@GiaTrenBaoBi numeric (18, 0)  ,

	@GiaBan numeric (18, 0)  ,

	@HinhAnh nvarchar (50)  ,

	@ThongTin nvarchar (200)  ,

	@MaLoai int   ,

	@MaNsx int   ,

	@SoLuong int   
)
AS


				
				INSERT INTO [dbo].[Sua]
					(
					[TenSua]
					,[GiaNhap]
					,[GiaTrenBaoBi]
					,[GiaBan]
					,[HinhAnh]
					,[ThongTin]
					,[MaLoai]
					,[MaNSX]
					,[SoLuong]
					)
				VALUES
					(
					@TenSua
					,@GiaNhap
					,@GiaTrenBaoBi
					,@GiaBan
					,@HinhAnh
					,@ThongTin
					,@MaLoai
					,@MaNsx
					,@SoLuong
					)
				
				-- Get the identity value
				SET @MaSua = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Sua table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_Update
(

	@MaSua int   ,

	@TenSua nvarchar (100)  ,

	@GiaNhap numeric (18, 0)  ,

	@GiaTrenBaoBi numeric (18, 0)  ,

	@GiaBan numeric (18, 0)  ,

	@HinhAnh nvarchar (50)  ,

	@ThongTin nvarchar (200)  ,

	@MaLoai int   ,

	@MaNsx int   ,

	@SoLuong int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Sua]
				SET
					[TenSua] = @TenSua
					,[GiaNhap] = @GiaNhap
					,[GiaTrenBaoBi] = @GiaTrenBaoBi
					,[GiaBan] = @GiaBan
					,[HinhAnh] = @HinhAnh
					,[ThongTin] = @ThongTin
					,[MaLoai] = @MaLoai
					,[MaNSX] = @MaNsx
					,[SoLuong] = @SoLuong
				WHERE
[MaSua] = @MaSua 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Sua table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_Delete
(

	@MaSua int   
)
AS


				DELETE FROM [dbo].[Sua] WITH (ROWLOCK) 
				WHERE
					[MaSua] = @MaSua
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_GetByMaLoai procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_GetByMaLoai') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_GetByMaLoai
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Sua table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_GetByMaLoai
(

	@MaLoai int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[MaSua],
					[TenSua],
					[GiaNhap],
					[GiaTrenBaoBi],
					[GiaBan],
					[HinhAnh],
					[ThongTin],
					[MaLoai],
					[MaNSX],
					[SoLuong]
				FROM
					[dbo].[Sua]
				WHERE
					[MaLoai] = @MaLoai
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_GetByMaNsx procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_GetByMaNsx') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_GetByMaNsx
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Sua table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_GetByMaNsx
(

	@MaNsx int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[MaSua],
					[TenSua],
					[GiaNhap],
					[GiaTrenBaoBi],
					[GiaBan],
					[HinhAnh],
					[ThongTin],
					[MaLoai],
					[MaNSX],
					[SoLuong]
				FROM
					[dbo].[Sua]
				WHERE
					[MaNSX] = @MaNsx
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_GetByMaSua procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_GetByMaSua') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_GetByMaSua
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Sua table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_GetByMaSua
(

	@MaSua int   
)
AS


				SELECT
					[MaSua],
					[TenSua],
					[GiaNhap],
					[GiaTrenBaoBi],
					[GiaBan],
					[HinhAnh],
					[ThongTin],
					[MaLoai],
					[MaNSX],
					[SoLuong]
				FROM
					[dbo].[Sua]
				WHERE
					[MaSua] = @MaSua
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Sua_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Sua_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Sua_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Sua table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Sua_Find
(

	@SearchUsingOR bit   = null ,

	@MaSua int   = null ,

	@TenSua nvarchar (100)  = null ,

	@GiaNhap numeric (18, 0)  = null ,

	@GiaTrenBaoBi numeric (18, 0)  = null ,

	@GiaBan numeric (18, 0)  = null ,

	@HinhAnh nvarchar (50)  = null ,

	@ThongTin nvarchar (200)  = null ,

	@MaLoai int   = null ,

	@MaNsx int   = null ,

	@SoLuong int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [MaSua]
	, [TenSua]
	, [GiaNhap]
	, [GiaTrenBaoBi]
	, [GiaBan]
	, [HinhAnh]
	, [ThongTin]
	, [MaLoai]
	, [MaNSX]
	, [SoLuong]
    FROM
	[dbo].[Sua]
    WHERE 
	 ([MaSua] = @MaSua OR @MaSua IS NULL)
	AND ([TenSua] = @TenSua OR @TenSua IS NULL)
	AND ([GiaNhap] = @GiaNhap OR @GiaNhap IS NULL)
	AND ([GiaTrenBaoBi] = @GiaTrenBaoBi OR @GiaTrenBaoBi IS NULL)
	AND ([GiaBan] = @GiaBan OR @GiaBan IS NULL)
	AND ([HinhAnh] = @HinhAnh OR @HinhAnh IS NULL)
	AND ([ThongTin] = @ThongTin OR @ThongTin IS NULL)
	AND ([MaLoai] = @MaLoai OR @MaLoai IS NULL)
	AND ([MaNSX] = @MaNsx OR @MaNsx IS NULL)
	AND ([SoLuong] = @SoLuong OR @SoLuong IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [MaSua]
	, [TenSua]
	, [GiaNhap]
	, [GiaTrenBaoBi]
	, [GiaBan]
	, [HinhAnh]
	, [ThongTin]
	, [MaLoai]
	, [MaNSX]
	, [SoLuong]
    FROM
	[dbo].[Sua]
    WHERE 
	 ([MaSua] = @MaSua AND @MaSua is not null)
	OR ([TenSua] = @TenSua AND @TenSua is not null)
	OR ([GiaNhap] = @GiaNhap AND @GiaNhap is not null)
	OR ([GiaTrenBaoBi] = @GiaTrenBaoBi AND @GiaTrenBaoBi is not null)
	OR ([GiaBan] = @GiaBan AND @GiaBan is not null)
	OR ([HinhAnh] = @HinhAnh AND @HinhAnh is not null)
	OR ([ThongTin] = @ThongTin AND @ThongTin is not null)
	OR ([MaLoai] = @MaLoai AND @MaLoai is not null)
	OR ([MaNSX] = @MaNsx AND @MaNsx is not null)
	OR ([SoLuong] = @SoLuong AND @SoLuong is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AnhChiTiet_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AnhChiTiet_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AnhChiTiet_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the AnhChiTiet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AnhChiTiet_Get_List

AS


				
				SELECT
					[MaAnh],
					[MaSua],
					[TenAnh]
				FROM
					[dbo].[AnhChiTiet]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AnhChiTiet_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AnhChiTiet_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AnhChiTiet_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the AnhChiTiet table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AnhChiTiet_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [MaAnh] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([MaAnh])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [MaAnh]'
				SET @SQL = @SQL + ' FROM [dbo].[AnhChiTiet]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[MaAnh], O.[MaSua], O.[TenAnh]
				FROM
				    [dbo].[AnhChiTiet] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[MaAnh] = PageIndex.[MaAnh]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[AnhChiTiet]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AnhChiTiet_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AnhChiTiet_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AnhChiTiet_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the AnhChiTiet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AnhChiTiet_Insert
(

	@MaAnh int    OUTPUT,

	@MaSua int   ,

	@TenAnh nvarchar (50)  
)
AS


				
				INSERT INTO [dbo].[AnhChiTiet]
					(
					[MaSua]
					,[TenAnh]
					)
				VALUES
					(
					@MaSua
					,@TenAnh
					)
				
				-- Get the identity value
				SET @MaAnh = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AnhChiTiet_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AnhChiTiet_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AnhChiTiet_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the AnhChiTiet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AnhChiTiet_Update
(

	@MaAnh int   ,

	@MaSua int   ,

	@TenAnh nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[AnhChiTiet]
				SET
					[MaSua] = @MaSua
					,[TenAnh] = @TenAnh
				WHERE
[MaAnh] = @MaAnh 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AnhChiTiet_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AnhChiTiet_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AnhChiTiet_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the AnhChiTiet table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AnhChiTiet_Delete
(

	@MaAnh int   
)
AS


				DELETE FROM [dbo].[AnhChiTiet] WITH (ROWLOCK) 
				WHERE
					[MaAnh] = @MaAnh
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AnhChiTiet_GetByMaSua procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AnhChiTiet_GetByMaSua') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AnhChiTiet_GetByMaSua
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the AnhChiTiet table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AnhChiTiet_GetByMaSua
(

	@MaSua int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[MaAnh],
					[MaSua],
					[TenAnh]
				FROM
					[dbo].[AnhChiTiet]
				WHERE
					[MaSua] = @MaSua
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AnhChiTiet_GetByMaAnh procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AnhChiTiet_GetByMaAnh') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AnhChiTiet_GetByMaAnh
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the AnhChiTiet table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AnhChiTiet_GetByMaAnh
(

	@MaAnh int   
)
AS


				SELECT
					[MaAnh],
					[MaSua],
					[TenAnh]
				FROM
					[dbo].[AnhChiTiet]
				WHERE
					[MaAnh] = @MaAnh
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.AnhChiTiet_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.AnhChiTiet_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.AnhChiTiet_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the AnhChiTiet table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.AnhChiTiet_Find
(

	@SearchUsingOR bit   = null ,

	@MaAnh int   = null ,

	@MaSua int   = null ,

	@TenAnh nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [MaAnh]
	, [MaSua]
	, [TenAnh]
    FROM
	[dbo].[AnhChiTiet]
    WHERE 
	 ([MaAnh] = @MaAnh OR @MaAnh IS NULL)
	AND ([MaSua] = @MaSua OR @MaSua IS NULL)
	AND ([TenAnh] = @TenAnh OR @TenAnh IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [MaAnh]
	, [MaSua]
	, [TenAnh]
    FROM
	[dbo].[AnhChiTiet]
    WHERE 
	 ([MaAnh] = @MaAnh AND @MaAnh is not null)
	OR ([MaSua] = @MaSua AND @MaSua is not null)
	OR ([TenAnh] = @TenAnh AND @TenAnh is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhaSanXuat_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhaSanXuat_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhaSanXuat_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the NhaSanXuat table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhaSanXuat_Get_List

AS


				
				SELECT
					[MaNSX],
					[TenNSX]
				FROM
					[dbo].[NhaSanXuat]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhaSanXuat_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhaSanXuat_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhaSanXuat_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the NhaSanXuat table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhaSanXuat_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [MaNSX] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([MaNSX])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [MaNSX]'
				SET @SQL = @SQL + ' FROM [dbo].[NhaSanXuat]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[MaNSX], O.[TenNSX]
				FROM
				    [dbo].[NhaSanXuat] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[MaNSX] = PageIndex.[MaNSX]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[NhaSanXuat]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhaSanXuat_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhaSanXuat_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhaSanXuat_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the NhaSanXuat table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhaSanXuat_Insert
(

	@MaNsx int    OUTPUT,

	@TenNsx nvarchar (100)  
)
AS


				
				INSERT INTO [dbo].[NhaSanXuat]
					(
					[TenNSX]
					)
				VALUES
					(
					@TenNsx
					)
				
				-- Get the identity value
				SET @MaNsx = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhaSanXuat_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhaSanXuat_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhaSanXuat_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the NhaSanXuat table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhaSanXuat_Update
(

	@MaNsx int   ,

	@TenNsx nvarchar (100)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[NhaSanXuat]
				SET
					[TenNSX] = @TenNsx
				WHERE
[MaNSX] = @MaNsx 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhaSanXuat_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhaSanXuat_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhaSanXuat_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the NhaSanXuat table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhaSanXuat_Delete
(

	@MaNsx int   
)
AS


				DELETE FROM [dbo].[NhaSanXuat] WITH (ROWLOCK) 
				WHERE
					[MaNSX] = @MaNsx
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhaSanXuat_GetByMaNsx procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhaSanXuat_GetByMaNsx') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhaSanXuat_GetByMaNsx
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the NhaSanXuat table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhaSanXuat_GetByMaNsx
(

	@MaNsx int   
)
AS


				SELECT
					[MaNSX],
					[TenNSX]
				FROM
					[dbo].[NhaSanXuat]
				WHERE
					[MaNSX] = @MaNsx
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhaSanXuat_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhaSanXuat_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhaSanXuat_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the NhaSanXuat table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhaSanXuat_Find
(

	@SearchUsingOR bit   = null ,

	@MaNsx int   = null ,

	@TenNsx nvarchar (100)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [MaNSX]
	, [TenNSX]
    FROM
	[dbo].[NhaSanXuat]
    WHERE 
	 ([MaNSX] = @MaNsx OR @MaNsx IS NULL)
	AND ([TenNSX] = @TenNsx OR @TenNsx IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [MaNSX]
	, [TenNSX]
    FROM
	[dbo].[NhaSanXuat]
    WHERE 
	 ([MaNSX] = @MaNsx AND @MaNsx is not null)
	OR ([TenNSX] = @TenNsx AND @TenNsx is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThanhVien_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThanhVien_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThanhVien_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the ThanhVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThanhVien_Get_List

AS


				
				SELECT
					[MaThanhVien],
					[TenDangNhap],
					[MatKhau],
					[HoTen],
					[DiaChi],
					[DienThoai],
					[Email],
					[Quyen]
				FROM
					[dbo].[ThanhVien]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThanhVien_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThanhVien_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThanhVien_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the ThanhVien table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThanhVien_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [MaThanhVien] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([MaThanhVien])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [MaThanhVien]'
				SET @SQL = @SQL + ' FROM [dbo].[ThanhVien]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[MaThanhVien], O.[TenDangNhap], O.[MatKhau], O.[HoTen], O.[DiaChi], O.[DienThoai], O.[Email], O.[Quyen]
				FROM
				    [dbo].[ThanhVien] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[MaThanhVien] = PageIndex.[MaThanhVien]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ThanhVien]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThanhVien_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThanhVien_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThanhVien_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the ThanhVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThanhVien_Insert
(

	@MaThanhVien int    OUTPUT,

	@TenDangNhap nvarchar (100)  ,

	@MatKhau nvarchar (50)  ,

	@HoTen nvarchar (100)  ,

	@DiaChi nvarchar (500)  ,

	@DienThoai nvarchar (11)  ,

	@Email nvarchar (100)  ,

	@Quyen int   
)
AS


				
				INSERT INTO [dbo].[ThanhVien]
					(
					[TenDangNhap]
					,[MatKhau]
					,[HoTen]
					,[DiaChi]
					,[DienThoai]
					,[Email]
					,[Quyen]
					)
				VALUES
					(
					@TenDangNhap
					,@MatKhau
					,@HoTen
					,@DiaChi
					,@DienThoai
					,@Email
					,@Quyen
					)
				
				-- Get the identity value
				SET @MaThanhVien = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThanhVien_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThanhVien_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThanhVien_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the ThanhVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThanhVien_Update
(

	@MaThanhVien int   ,

	@TenDangNhap nvarchar (100)  ,

	@MatKhau nvarchar (50)  ,

	@HoTen nvarchar (100)  ,

	@DiaChi nvarchar (500)  ,

	@DienThoai nvarchar (11)  ,

	@Email nvarchar (100)  ,

	@Quyen int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ThanhVien]
				SET
					[TenDangNhap] = @TenDangNhap
					,[MatKhau] = @MatKhau
					,[HoTen] = @HoTen
					,[DiaChi] = @DiaChi
					,[DienThoai] = @DienThoai
					,[Email] = @Email
					,[Quyen] = @Quyen
				WHERE
[MaThanhVien] = @MaThanhVien 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThanhVien_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThanhVien_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThanhVien_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the ThanhVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThanhVien_Delete
(

	@MaThanhVien int   
)
AS


				DELETE FROM [dbo].[ThanhVien] WITH (ROWLOCK) 
				WHERE
					[MaThanhVien] = @MaThanhVien
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThanhVien_GetByMaThanhVien procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThanhVien_GetByMaThanhVien') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThanhVien_GetByMaThanhVien
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the ThanhVien table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThanhVien_GetByMaThanhVien
(

	@MaThanhVien int   
)
AS


				SELECT
					[MaThanhVien],
					[TenDangNhap],
					[MatKhau],
					[HoTen],
					[DiaChi],
					[DienThoai],
					[Email],
					[Quyen]
				FROM
					[dbo].[ThanhVien]
				WHERE
					[MaThanhVien] = @MaThanhVien
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThanhVien_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThanhVien_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThanhVien_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the ThanhVien table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThanhVien_Find
(

	@SearchUsingOR bit   = null ,

	@MaThanhVien int   = null ,

	@TenDangNhap nvarchar (100)  = null ,

	@MatKhau nvarchar (50)  = null ,

	@HoTen nvarchar (100)  = null ,

	@DiaChi nvarchar (500)  = null ,

	@DienThoai nvarchar (11)  = null ,

	@Email nvarchar (100)  = null ,

	@Quyen int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [MaThanhVien]
	, [TenDangNhap]
	, [MatKhau]
	, [HoTen]
	, [DiaChi]
	, [DienThoai]
	, [Email]
	, [Quyen]
    FROM
	[dbo].[ThanhVien]
    WHERE 
	 ([MaThanhVien] = @MaThanhVien OR @MaThanhVien IS NULL)
	AND ([TenDangNhap] = @TenDangNhap OR @TenDangNhap IS NULL)
	AND ([MatKhau] = @MatKhau OR @MatKhau IS NULL)
	AND ([HoTen] = @HoTen OR @HoTen IS NULL)
	AND ([DiaChi] = @DiaChi OR @DiaChi IS NULL)
	AND ([DienThoai] = @DienThoai OR @DienThoai IS NULL)
	AND ([Email] = @Email OR @Email IS NULL)
	AND ([Quyen] = @Quyen OR @Quyen IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [MaThanhVien]
	, [TenDangNhap]
	, [MatKhau]
	, [HoTen]
	, [DiaChi]
	, [DienThoai]
	, [Email]
	, [Quyen]
    FROM
	[dbo].[ThanhVien]
    WHERE 
	 ([MaThanhVien] = @MaThanhVien AND @MaThanhVien is not null)
	OR ([TenDangNhap] = @TenDangNhap AND @TenDangNhap is not null)
	OR ([MatKhau] = @MatKhau AND @MatKhau is not null)
	OR ([HoTen] = @HoTen AND @HoTen is not null)
	OR ([DiaChi] = @DiaChi AND @DiaChi is not null)
	OR ([DienThoai] = @DienThoai AND @DienThoai is not null)
	OR ([Email] = @Email AND @Email is not null)
	OR ([Quyen] = @Quyen AND @Quyen is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhanVien_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhanVien_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhanVien_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the NhanVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhanVien_Get_List

AS


				
				SELECT
					[MaNhanVien],
					[TenDangNhap],
					[MatKhau],
					[TenNhanVien],
					[NgayVaoLam],
					[DienThoai],
					[DiaChi],
					[Email],
					[CMND]
				FROM
					[dbo].[NhanVien]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhanVien_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhanVien_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhanVien_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the NhanVien table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhanVien_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [MaNhanVien] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([MaNhanVien])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [MaNhanVien]'
				SET @SQL = @SQL + ' FROM [dbo].[NhanVien]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[MaNhanVien], O.[TenDangNhap], O.[MatKhau], O.[TenNhanVien], O.[NgayVaoLam], O.[DienThoai], O.[DiaChi], O.[Email], O.[CMND]
				FROM
				    [dbo].[NhanVien] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[MaNhanVien] = PageIndex.[MaNhanVien]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[NhanVien]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhanVien_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhanVien_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhanVien_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the NhanVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhanVien_Insert
(

	@MaNhanVien int    OUTPUT,

	@TenDangNhap nvarchar (100)  ,

	@MatKhau nvarchar (50)  ,

	@TenNhanVien nvarchar (100)  ,

	@NgayVaoLam datetime   ,

	@DienThoai nvarchar (11)  ,

	@DiaChi nvarchar (200)  ,

	@Email nvarchar (100)  ,

	@Cmnd nvarchar (14)  
)
AS


				
				INSERT INTO [dbo].[NhanVien]
					(
					[TenDangNhap]
					,[MatKhau]
					,[TenNhanVien]
					,[NgayVaoLam]
					,[DienThoai]
					,[DiaChi]
					,[Email]
					,[CMND]
					)
				VALUES
					(
					@TenDangNhap
					,@MatKhau
					,@TenNhanVien
					,@NgayVaoLam
					,@DienThoai
					,@DiaChi
					,@Email
					,@Cmnd
					)
				
				-- Get the identity value
				SET @MaNhanVien = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhanVien_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhanVien_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhanVien_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the NhanVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhanVien_Update
(

	@MaNhanVien int   ,

	@TenDangNhap nvarchar (100)  ,

	@MatKhau nvarchar (50)  ,

	@TenNhanVien nvarchar (100)  ,

	@NgayVaoLam datetime   ,

	@DienThoai nvarchar (11)  ,

	@DiaChi nvarchar (200)  ,

	@Email nvarchar (100)  ,

	@Cmnd nvarchar (14)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[NhanVien]
				SET
					[TenDangNhap] = @TenDangNhap
					,[MatKhau] = @MatKhau
					,[TenNhanVien] = @TenNhanVien
					,[NgayVaoLam] = @NgayVaoLam
					,[DienThoai] = @DienThoai
					,[DiaChi] = @DiaChi
					,[Email] = @Email
					,[CMND] = @Cmnd
				WHERE
[MaNhanVien] = @MaNhanVien 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhanVien_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhanVien_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhanVien_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the NhanVien table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhanVien_Delete
(

	@MaNhanVien int   
)
AS


				DELETE FROM [dbo].[NhanVien] WITH (ROWLOCK) 
				WHERE
					[MaNhanVien] = @MaNhanVien
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhanVien_GetByMaNhanVien procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhanVien_GetByMaNhanVien') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhanVien_GetByMaNhanVien
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the NhanVien table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhanVien_GetByMaNhanVien
(

	@MaNhanVien int   
)
AS


				SELECT
					[MaNhanVien],
					[TenDangNhap],
					[MatKhau],
					[TenNhanVien],
					[NgayVaoLam],
					[DienThoai],
					[DiaChi],
					[Email],
					[CMND]
				FROM
					[dbo].[NhanVien]
				WHERE
					[MaNhanVien] = @MaNhanVien
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.NhanVien_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.NhanVien_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.NhanVien_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the NhanVien table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.NhanVien_Find
(

	@SearchUsingOR bit   = null ,

	@MaNhanVien int   = null ,

	@TenDangNhap nvarchar (100)  = null ,

	@MatKhau nvarchar (50)  = null ,

	@TenNhanVien nvarchar (100)  = null ,

	@NgayVaoLam datetime   = null ,

	@DienThoai nvarchar (11)  = null ,

	@DiaChi nvarchar (200)  = null ,

	@Email nvarchar (100)  = null ,

	@Cmnd nvarchar (14)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [MaNhanVien]
	, [TenDangNhap]
	, [MatKhau]
	, [TenNhanVien]
	, [NgayVaoLam]
	, [DienThoai]
	, [DiaChi]
	, [Email]
	, [CMND]
    FROM
	[dbo].[NhanVien]
    WHERE 
	 ([MaNhanVien] = @MaNhanVien OR @MaNhanVien IS NULL)
	AND ([TenDangNhap] = @TenDangNhap OR @TenDangNhap IS NULL)
	AND ([MatKhau] = @MatKhau OR @MatKhau IS NULL)
	AND ([TenNhanVien] = @TenNhanVien OR @TenNhanVien IS NULL)
	AND ([NgayVaoLam] = @NgayVaoLam OR @NgayVaoLam IS NULL)
	AND ([DienThoai] = @DienThoai OR @DienThoai IS NULL)
	AND ([DiaChi] = @DiaChi OR @DiaChi IS NULL)
	AND ([Email] = @Email OR @Email IS NULL)
	AND ([CMND] = @Cmnd OR @Cmnd IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [MaNhanVien]
	, [TenDangNhap]
	, [MatKhau]
	, [TenNhanVien]
	, [NgayVaoLam]
	, [DienThoai]
	, [DiaChi]
	, [Email]
	, [CMND]
    FROM
	[dbo].[NhanVien]
    WHERE 
	 ([MaNhanVien] = @MaNhanVien AND @MaNhanVien is not null)
	OR ([TenDangNhap] = @TenDangNhap AND @TenDangNhap is not null)
	OR ([MatKhau] = @MatKhau AND @MatKhau is not null)
	OR ([TenNhanVien] = @TenNhanVien AND @TenNhanVien is not null)
	OR ([NgayVaoLam] = @NgayVaoLam AND @NgayVaoLam is not null)
	OR ([DienThoai] = @DienThoai AND @DienThoai is not null)
	OR ([DiaChi] = @DiaChi AND @DiaChi is not null)
	OR ([Email] = @Email AND @Email is not null)
	OR ([CMND] = @Cmnd AND @Cmnd is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the HoaDon table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_Get_List

AS


				
				SELECT
					[MaHoaDon],
					[MaThanhVien],
					[NgayDatHang],
					[TriGia],
					[DaGiaoHang],
					[NgayGiaoHang],
					[DiaChiGiaoHang],
					[PhiGiaoHang],
					[TinhTrang],
					[MaNhanVien]
				FROM
					[dbo].[HoaDon]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the HoaDon table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [MaHoaDon] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([MaHoaDon])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [MaHoaDon]'
				SET @SQL = @SQL + ' FROM [dbo].[HoaDon]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[MaHoaDon], O.[MaThanhVien], O.[NgayDatHang], O.[TriGia], O.[DaGiaoHang], O.[NgayGiaoHang], O.[DiaChiGiaoHang], O.[PhiGiaoHang], O.[TinhTrang], O.[MaNhanVien]
				FROM
				    [dbo].[HoaDon] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[MaHoaDon] = PageIndex.[MaHoaDon]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[HoaDon]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the HoaDon table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_Insert
(

	@MaHoaDon int    OUTPUT,

	@MaThanhVien int   ,

	@NgayDatHang datetime   ,

	@TriGia int   ,

	@DaGiaoHang bit   ,

	@NgayGiaoHang datetime   ,

	@DiaChiGiaoHang nvarchar (200)  ,

	@PhiGiaoHang numeric (18, 0)  ,

	@TinhTrang int   ,

	@MaNhanVien int   
)
AS


				
				INSERT INTO [dbo].[HoaDon]
					(
					[MaThanhVien]
					,[NgayDatHang]
					,[TriGia]
					,[DaGiaoHang]
					,[NgayGiaoHang]
					,[DiaChiGiaoHang]
					,[PhiGiaoHang]
					,[TinhTrang]
					,[MaNhanVien]
					)
				VALUES
					(
					@MaThanhVien
					,@NgayDatHang
					,@TriGia
					,@DaGiaoHang
					,@NgayGiaoHang
					,@DiaChiGiaoHang
					,@PhiGiaoHang
					,@TinhTrang
					,@MaNhanVien
					)
				
				-- Get the identity value
				SET @MaHoaDon = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the HoaDon table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_Update
(

	@MaHoaDon int   ,

	@MaThanhVien int   ,

	@NgayDatHang datetime   ,

	@TriGia int   ,

	@DaGiaoHang bit   ,

	@NgayGiaoHang datetime   ,

	@DiaChiGiaoHang nvarchar (200)  ,

	@PhiGiaoHang numeric (18, 0)  ,

	@TinhTrang int   ,

	@MaNhanVien int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[HoaDon]
				SET
					[MaThanhVien] = @MaThanhVien
					,[NgayDatHang] = @NgayDatHang
					,[TriGia] = @TriGia
					,[DaGiaoHang] = @DaGiaoHang
					,[NgayGiaoHang] = @NgayGiaoHang
					,[DiaChiGiaoHang] = @DiaChiGiaoHang
					,[PhiGiaoHang] = @PhiGiaoHang
					,[TinhTrang] = @TinhTrang
					,[MaNhanVien] = @MaNhanVien
				WHERE
[MaHoaDon] = @MaHoaDon 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the HoaDon table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_Delete
(

	@MaHoaDon int   
)
AS


				DELETE FROM [dbo].[HoaDon] WITH (ROWLOCK) 
				WHERE
					[MaHoaDon] = @MaHoaDon
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_GetByMaNhanVien procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_GetByMaNhanVien') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_GetByMaNhanVien
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the HoaDon table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_GetByMaNhanVien
(

	@MaNhanVien int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[MaHoaDon],
					[MaThanhVien],
					[NgayDatHang],
					[TriGia],
					[DaGiaoHang],
					[NgayGiaoHang],
					[DiaChiGiaoHang],
					[PhiGiaoHang],
					[TinhTrang],
					[MaNhanVien]
				FROM
					[dbo].[HoaDon]
				WHERE
					[MaNhanVien] = @MaNhanVien
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_GetByMaThanhVien procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_GetByMaThanhVien') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_GetByMaThanhVien
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the HoaDon table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_GetByMaThanhVien
(

	@MaThanhVien int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[MaHoaDon],
					[MaThanhVien],
					[NgayDatHang],
					[TriGia],
					[DaGiaoHang],
					[NgayGiaoHang],
					[DiaChiGiaoHang],
					[PhiGiaoHang],
					[TinhTrang],
					[MaNhanVien]
				FROM
					[dbo].[HoaDon]
				WHERE
					[MaThanhVien] = @MaThanhVien
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_GetByMaHoaDon procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_GetByMaHoaDon') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_GetByMaHoaDon
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the HoaDon table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_GetByMaHoaDon
(

	@MaHoaDon int   
)
AS


				SELECT
					[MaHoaDon],
					[MaThanhVien],
					[NgayDatHang],
					[TriGia],
					[DaGiaoHang],
					[NgayGiaoHang],
					[DiaChiGiaoHang],
					[PhiGiaoHang],
					[TinhTrang],
					[MaNhanVien]
				FROM
					[dbo].[HoaDon]
				WHERE
					[MaHoaDon] = @MaHoaDon
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.HoaDon_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.HoaDon_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.HoaDon_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the HoaDon table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.HoaDon_Find
(

	@SearchUsingOR bit   = null ,

	@MaHoaDon int   = null ,

	@MaThanhVien int   = null ,

	@NgayDatHang datetime   = null ,

	@TriGia int   = null ,

	@DaGiaoHang bit   = null ,

	@NgayGiaoHang datetime   = null ,

	@DiaChiGiaoHang nvarchar (200)  = null ,

	@PhiGiaoHang numeric (18, 0)  = null ,

	@TinhTrang int   = null ,

	@MaNhanVien int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [MaHoaDon]
	, [MaThanhVien]
	, [NgayDatHang]
	, [TriGia]
	, [DaGiaoHang]
	, [NgayGiaoHang]
	, [DiaChiGiaoHang]
	, [PhiGiaoHang]
	, [TinhTrang]
	, [MaNhanVien]
    FROM
	[dbo].[HoaDon]
    WHERE 
	 ([MaHoaDon] = @MaHoaDon OR @MaHoaDon IS NULL)
	AND ([MaThanhVien] = @MaThanhVien OR @MaThanhVien IS NULL)
	AND ([NgayDatHang] = @NgayDatHang OR @NgayDatHang IS NULL)
	AND ([TriGia] = @TriGia OR @TriGia IS NULL)
	AND ([DaGiaoHang] = @DaGiaoHang OR @DaGiaoHang IS NULL)
	AND ([NgayGiaoHang] = @NgayGiaoHang OR @NgayGiaoHang IS NULL)
	AND ([DiaChiGiaoHang] = @DiaChiGiaoHang OR @DiaChiGiaoHang IS NULL)
	AND ([PhiGiaoHang] = @PhiGiaoHang OR @PhiGiaoHang IS NULL)
	AND ([TinhTrang] = @TinhTrang OR @TinhTrang IS NULL)
	AND ([MaNhanVien] = @MaNhanVien OR @MaNhanVien IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [MaHoaDon]
	, [MaThanhVien]
	, [NgayDatHang]
	, [TriGia]
	, [DaGiaoHang]
	, [NgayGiaoHang]
	, [DiaChiGiaoHang]
	, [PhiGiaoHang]
	, [TinhTrang]
	, [MaNhanVien]
    FROM
	[dbo].[HoaDon]
    WHERE 
	 ([MaHoaDon] = @MaHoaDon AND @MaHoaDon is not null)
	OR ([MaThanhVien] = @MaThanhVien AND @MaThanhVien is not null)
	OR ([NgayDatHang] = @NgayDatHang AND @NgayDatHang is not null)
	OR ([TriGia] = @TriGia AND @TriGia is not null)
	OR ([DaGiaoHang] = @DaGiaoHang AND @DaGiaoHang is not null)
	OR ([NgayGiaoHang] = @NgayGiaoHang AND @NgayGiaoHang is not null)
	OR ([DiaChiGiaoHang] = @DiaChiGiaoHang AND @DiaChiGiaoHang is not null)
	OR ([PhiGiaoHang] = @PhiGiaoHang AND @PhiGiaoHang is not null)
	OR ([TinhTrang] = @TinhTrang AND @TinhTrang is not null)
	OR ([MaNhanVien] = @MaNhanVien AND @MaNhanVien is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TinTuc_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TinTuc_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TinTuc_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the TinTuc table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TinTuc_Get_List

AS


				
				SELECT
					[MaTinTuc],
					[TieuDe],
					[HinhAnh],
					[ThongTinAnh],
					[ThongTonChinh]
				FROM
					[dbo].[TinTuc]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TinTuc_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TinTuc_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TinTuc_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the TinTuc table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TinTuc_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [MaTinTuc] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([MaTinTuc])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [MaTinTuc]'
				SET @SQL = @SQL + ' FROM [dbo].[TinTuc]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[MaTinTuc], O.[TieuDe], O.[HinhAnh], O.[ThongTinAnh], O.[ThongTonChinh]
				FROM
				    [dbo].[TinTuc] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[MaTinTuc] = PageIndex.[MaTinTuc]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[TinTuc]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TinTuc_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TinTuc_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TinTuc_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the TinTuc table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TinTuc_Insert
(

	@MaTinTuc int    OUTPUT,

	@TieuDe nvarchar (100)  ,

	@HinhAnh nvarchar (50)  ,

	@ThongTinAnh nvarchar (1000)  ,

	@ThongTonChinh nvarchar (4000)  
)
AS


				
				INSERT INTO [dbo].[TinTuc]
					(
					[TieuDe]
					,[HinhAnh]
					,[ThongTinAnh]
					,[ThongTonChinh]
					)
				VALUES
					(
					@TieuDe
					,@HinhAnh
					,@ThongTinAnh
					,@ThongTonChinh
					)
				
				-- Get the identity value
				SET @MaTinTuc = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TinTuc_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TinTuc_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TinTuc_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the TinTuc table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TinTuc_Update
(

	@MaTinTuc int   ,

	@TieuDe nvarchar (100)  ,

	@HinhAnh nvarchar (50)  ,

	@ThongTinAnh nvarchar (1000)  ,

	@ThongTonChinh nvarchar (4000)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[TinTuc]
				SET
					[TieuDe] = @TieuDe
					,[HinhAnh] = @HinhAnh
					,[ThongTinAnh] = @ThongTinAnh
					,[ThongTonChinh] = @ThongTonChinh
				WHERE
[MaTinTuc] = @MaTinTuc 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TinTuc_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TinTuc_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TinTuc_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the TinTuc table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TinTuc_Delete
(

	@MaTinTuc int   
)
AS


				DELETE FROM [dbo].[TinTuc] WITH (ROWLOCK) 
				WHERE
					[MaTinTuc] = @MaTinTuc
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TinTuc_GetByMaTinTuc procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TinTuc_GetByMaTinTuc') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TinTuc_GetByMaTinTuc
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the TinTuc table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TinTuc_GetByMaTinTuc
(

	@MaTinTuc int   
)
AS


				SELECT
					[MaTinTuc],
					[TieuDe],
					[HinhAnh],
					[ThongTinAnh],
					[ThongTonChinh]
				FROM
					[dbo].[TinTuc]
				WHERE
					[MaTinTuc] = @MaTinTuc
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TinTuc_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TinTuc_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TinTuc_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the TinTuc table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TinTuc_Find
(

	@SearchUsingOR bit   = null ,

	@MaTinTuc int   = null ,

	@TieuDe nvarchar (100)  = null ,

	@HinhAnh nvarchar (50)  = null ,

	@ThongTinAnh nvarchar (1000)  = null ,

	@ThongTonChinh nvarchar (4000)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [MaTinTuc]
	, [TieuDe]
	, [HinhAnh]
	, [ThongTinAnh]
	, [ThongTonChinh]
    FROM
	[dbo].[TinTuc]
    WHERE 
	 ([MaTinTuc] = @MaTinTuc OR @MaTinTuc IS NULL)
	AND ([TieuDe] = @TieuDe OR @TieuDe IS NULL)
	AND ([HinhAnh] = @HinhAnh OR @HinhAnh IS NULL)
	AND ([ThongTinAnh] = @ThongTinAnh OR @ThongTinAnh IS NULL)
	AND ([ThongTonChinh] = @ThongTonChinh OR @ThongTonChinh IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [MaTinTuc]
	, [TieuDe]
	, [HinhAnh]
	, [ThongTinAnh]
	, [ThongTonChinh]
    FROM
	[dbo].[TinTuc]
    WHERE 
	 ([MaTinTuc] = @MaTinTuc AND @MaTinTuc is not null)
	OR ([TieuDe] = @TieuDe AND @TieuDe is not null)
	OR ([HinhAnh] = @HinhAnh AND @HinhAnh is not null)
	OR ([ThongTinAnh] = @ThongTinAnh AND @ThongTinAnh is not null)
	OR ([ThongTonChinh] = @ThongTonChinh AND @ThongTonChinh is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiSua_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiSua_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiSua_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the LoaiSua table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiSua_Get_List

AS


				
				SELECT
					[MaLoai],
					[TenLoai]
				FROM
					[dbo].[LoaiSua]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiSua_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiSua_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiSua_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the LoaiSua table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiSua_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [MaLoai] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([MaLoai])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [MaLoai]'
				SET @SQL = @SQL + ' FROM [dbo].[LoaiSua]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[MaLoai], O.[TenLoai]
				FROM
				    [dbo].[LoaiSua] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[MaLoai] = PageIndex.[MaLoai]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[LoaiSua]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiSua_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiSua_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiSua_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the LoaiSua table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiSua_Insert
(

	@MaLoai int    OUTPUT,

	@TenLoai nvarchar (50)  
)
AS


				
				INSERT INTO [dbo].[LoaiSua]
					(
					[TenLoai]
					)
				VALUES
					(
					@TenLoai
					)
				
				-- Get the identity value
				SET @MaLoai = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiSua_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiSua_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiSua_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the LoaiSua table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiSua_Update
(

	@MaLoai int   ,

	@TenLoai nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[LoaiSua]
				SET
					[TenLoai] = @TenLoai
				WHERE
[MaLoai] = @MaLoai 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiSua_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiSua_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiSua_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the LoaiSua table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiSua_Delete
(

	@MaLoai int   
)
AS


				DELETE FROM [dbo].[LoaiSua] WITH (ROWLOCK) 
				WHERE
					[MaLoai] = @MaLoai
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiSua_GetByMaLoai procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiSua_GetByMaLoai') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiSua_GetByMaLoai
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the LoaiSua table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiSua_GetByMaLoai
(

	@MaLoai int   
)
AS


				SELECT
					[MaLoai],
					[TenLoai]
				FROM
					[dbo].[LoaiSua]
				WHERE
					[MaLoai] = @MaLoai
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LoaiSua_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LoaiSua_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LoaiSua_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the LoaiSua table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LoaiSua_Find
(

	@SearchUsingOR bit   = null ,

	@MaLoai int   = null ,

	@TenLoai nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [MaLoai]
	, [TenLoai]
    FROM
	[dbo].[LoaiSua]
    WHERE 
	 ([MaLoai] = @MaLoai OR @MaLoai IS NULL)
	AND ([TenLoai] = @TenLoai OR @TenLoai IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [MaLoai]
	, [TenLoai]
    FROM
	[dbo].[LoaiSua]
    WHERE 
	 ([MaLoai] = @MaLoai AND @MaLoai is not null)
	OR ([TenLoai] = @TenLoai AND @TenLoai is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

